<?php

/**
 * MyQEE ORM 数据库核心类
 *
 * @author     jonwang(jonwang@myqee.com)
 * @category   MyQEE
 * @package    System
 * @subpackage Core
 * @copyright  Copyright (c) 2008-2011 myqee.com
 * @license    http://www.myqee.com/license.html
 */
class MyQEE_OOP_ORM_Finder_DB extends OOP_ORM
{

    /**
     * 数据库配置名
     * @var string
     */
    protected $database = 'default';

    /**
     * 表名称
     * @var string
     */
    protected $tablename;

    function __construct()
    {
        if ( null === $this->tablename )
        {
            throw new Exception(__('orm tablename is not declared.'));
        }
        parent::__construct();
    }

    /**
     * 获取数据库名称
     */
    public function database()
    {
        return $this->database;
    }

    /**
     * 获取表名称
     */
    public function tablename()
    {
        return $this->tablename;
    }

    /**
     * 获取数据
     *
     * @param $query SQL
     * @return OOP_ORM_Result
     */
    public function find($sql = null)
    {
        if ( $sql )
        {
            $data = $this->driver()->query($sql)->as_array();
        }
        else
        {
            $bulider = $this->driver()->get_builder();
            if ( ! $bulider['from'] )
            {
                $this->driver()->from($this->tablename);
            }
            if ( $this->_auto_where )
            {
                $this->driver()->where($this->_auto_where);
            }
            $data = $this->driver()->get()->as_array();
        }

        # 最后查询SQL
        $this->last_query = $this->driver()->last_query();

        $obj = $this->create_group_data($data, true);

        return $obj;
    }

    /**
     * 返回数据库对象
     *
     * @return Database
     */
    public function driver()
    {
        return Database::instance($this->database);
    }

    /**
     * 返回数据库对象
     *
     * 兼容Model的写法
     *
     * @return Database
     */
    public function db()
    {
        return $this->driver();
    }

    /**
     *
     * @param   mixed   column name or array($column, $alias) or object
     * @param   string  logic operator
     * @param   mixed   column value
     * @return  MyQEE_OOP_ORM_Finder_DB
     */
    public function where($column, $value = null, $op = '=')
    {
        $this->driver()->where($column, $value, $op);
        return $this;
    }

    /**
     * Applies sorting with "ORDER BY ..."
     *
     * @param   mixed   column name or array($column, $alias) or object
     * @param   string  direction of sorting
     * @return  Database
     */
    public function order_by($column, $direction = 'ASC')
    {
        $this->driver()->order_by($column, $direction);
        return $this;
    }

    /**
     * Start returning results after "OFFSET ..."
     *
     * @param   integer   starting result number
     * @return  MyQEE_OOP_ORM_Finder_DB
     */
    public function offset($number)
    {
        $this->driver()->offset($number);
        return $this;
    }

    /**
     * Return up to "LIMIT ..." results
     *
     * @param   integer  maximum results to return
     * @return  MyQEE_OOP_ORM_Finder_DB
     */
    public function limit($number)
    {
        $this->driver()->limit($number);
        return $this;
    }

    /**
     *
     * @param string $key
     * @param array $value
     * @return Database
     */
    public function in($column, $value, $no_in = false)
    {
        $this->driver()->in($column, $value, $no_in);
        return $this;
    }

    /**
     * 插入数据
     * @param string $table
     * @param array $value
     * @param Database_Result
     * @return array(插入ID,作用行数)
     */
    public function insert($value = null)
    {
        return $this->driver()->insert($this->tablename(), $value);
    }

    /**
     * 更新数据
     * @param string $table
     * @param array $value
     * @param array $where
     * @return int 作用的行数
     */
    public function update($value = null, $where = null)
    {
        return $this->driver()->update($this->tablename(), $value, $where);
    }

    /**
     * 删除数据
     * @param string $table
     * @param array $where
     * @return int 作用的行数
     */
    public function delete($where = null)
    {
        return $this->driver()->delete($this->tablename(), $where);
    }
}

